/*
xhkogvdqif
xhkogvd___
用时9s
正确率0.700000
*/
#include <termios.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define LEN 10

char mygetch(void);
void my_print(char *str, int i);

int main(int argc, char **argv)
{
    char str[32] = "";
    int cnt = 0;
    int start_time;
    int edn_time;

    // 设置随机数种子
    srand(time(NULL));

    char ch = '\0';
    int i = 0;
    for (i = 0; i < LEN; i++)
    {
        ch = 'a' + rand() % 26;
        printf("%c", ch);
        str[i] = ch;
    }
    printf("\n");

    //printf("%s\n", str);
    start_time = time(NULL);


    for (i = 0; i < LEN; i++)
    {
        ch = mygetch();
        if (ch != str[i])
        {
            str[i] = '_';
            cnt++;
            //my_print(str, i);
        }
        my_print(str, i);
    }
    edn_time = time(NULL);
    
    printf("\n用时%ds\n", (int)(edn_time - start_time));
    printf("正确率%lf\n", (double)((10 - cnt) / 10.0));
    return 0;
}

char mygetch(void)
{
    struct termios oldt, newt;
    char ch;
    tcgetattr( STDIN_FILENO, &oldt );
    newt = oldt;
    newt.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newt );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
    return ch;
}

void my_print(char *str, int i)
{
    int n = 0;
    printf("\r");
    for (n = 0; n <= i; n++)
    {
        printf("%c", str[n]);
    }
    fflush(stdout);
    return;
}